﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WealowsHospital.BLL
{
    class WardBus
    {
        WellmeadowsEntities hospital;
        public WardBus(WellmeadowsEntities hospital)
        {
            this.hospital = hospital;
        }
        /// <summary>
        /// Phan nhan vien cho ward
        /// </summary>
        /// <param name="startOfWeek">Ngay bat dau cua tuan</param>
        /// <param name="wardId">Ward</param>
        /// <param name="nurseId">Nurse chinh</param>
        /// <param name="staffId">NVien</param>
        /// <param name="shift">ca</param>
        public bool AssignStaff(DateTime startOfWeek, string wardId, string nurseId, string staffId, string shift)
        {
            //lay thong tin ve ward
            Ward ward = hospital.Ward.Where(wd => wd.WardID == wardId).FirstOrDefault();
            Staff nurse = hospital.Staff.Where(st => st.StaffID == nurseId).FirstOrDefault();
            Staff staff = hospital.Staff.Where(st => st.StaffID == staffId).FirstOrDefault();

            WardStaffAllocation allocation = new WardStaffAllocation()
            {
                weekBegin=startOfWeek,
                Ward=ward,
                Staff=nurse
            };
            SattWardAllocation staffAllocation = new SattWardAllocation()
            {
                shift=shift,
                Staff=staff,
                WardStaffAllocation=allocation
            };

            hospital.AddToWardStaffAllocation(allocation);
            hospital.AddToSattWardAllocation(staffAllocation);

            return hospital.SaveChanges() > 0;
        }

        //get current staff in ward
        public List<Staff> GetCurrentStaffInWard(string wardId,DateTime startWeek)
        {
            Ward selectedWard=hospital.Ward.Where(w=>w.WardID==wardId).FirstOrDefault();
            List<WardStaffAllocation> allocation = hospital.WardStaffAllocation.Where(alloc => alloc.Ward == selectedWard).ToList();
            List<Staff> tmpStaff = new List<Staff>();
            foreach (WardStaffAllocation anAllocation in allocation)
            {
                tmpStaff.Add(anAllocation.SattWardAllocation.Staff);
            }
            return tmpStaff;
        }
    }
}
